﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using AmkeniCrossLoginApplication.Data_Access;

namespace Amkeni_Monitoring_Plan.Classes
{
    public class MonitoringPlanDAO
    {
        public IList<monitoring_plan_view> GetPlans()
        {
            return GetMonitoringPlans(string.Empty);
        }

        public IList<monitoring_plan_view> GetMonitoringPlans(string strQuery)
        {
            SqlConnection SQLconn = null;
            //EntityConnection conn = null;
            try
            {
                SQLconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                //conn = new EntityConnection("name=loa_mcg_databaseEntities");
                if (strQuery == string.Empty)
                {
                    strQuery = "SELECT * FROM monitoring_plan_view ";
                }
                SqlCommand SQLcmd = new SqlCommand();
                SQLcmd.CommandText = strQuery;
                SQLcmd.Connection = SQLconn;

                SQLcmd.Connection.Open();
                SqlDataReader SQLdataReader = SQLcmd.ExecuteReader(CommandBehavior.CloseConnection);

                IList<monitoring_plan_view> SQLMonitoringPlanViewList = new List<monitoring_plan_view>();

                while (SQLdataReader.Read())
                {
                    monitoring_plan_view monitoringPlanView = new monitoring_plan_view();
                    monitoringPlanView.monitoring_plan_id = Convert.ToInt32(SQLdataReader["monitoring_plan_id"].ToString());
                    monitoringPlanView.indicator_id = Convert.ToInt32(SQLdataReader["indicator_id"].ToString());
                    monitoringPlanView.call = SQLdataReader["call"].ToString();
                    monitoringPlanView.abbreviation = SQLdataReader["abbreviation"].ToString();
                    monitoringPlanView.outcome_id = Convert.ToInt32(SQLdataReader["outcome_id"].ToString());
                    monitoringPlanView.output_id = SQLdataReader["output_id"].ToString();
                    monitoringPlanView.activity = SQLdataReader["activity"].ToString();
                    monitoringPlanView.output_indicator_id = SQLdataReader["output_indicator_id"].ToString();
                    monitoringPlanView.baseline = SQLdataReader["baseline"].ToString();
                    monitoringPlanView.target = SQLdataReader["target"].ToString();
                    monitoringPlanView.programme_officer = SQLdataReader["programme_officer"].ToString();
                    monitoringPlanView.ip_officer = SQLdataReader["ip_officer"].ToString();
                    monitoringPlanView.data_source = SQLdataReader["data_source"].ToString();
                    monitoringPlanView.method_of_collection = SQLdataReader["method_of_collection"].ToString();
                    monitoringPlanView.collection_tools = SQLdataReader["collection_tools"].ToString();
                    monitoringPlanView.year = Convert.ToInt32(SQLdataReader["year"].ToString());
                    monitoringPlanView.resource_required = Convert.ToDecimal(SQLdataReader["resource_required"].ToString());
                    monitoringPlanView.assumptions_and_risks = SQLdataReader["assumptions_and_risks"].ToString();
                    monitoringPlanView.means_of_verification = SQLdataReader["means_of_verification"].ToString();
                    monitoringPlanView.outcome_name = SQLdataReader["outcome_name"].ToString();
                    monitoringPlanView.output_name = SQLdataReader["output_name"].ToString();
                    monitoringPlanView.indicator_name = SQLdataReader["indicator_name"].ToString();

                    SQLMonitoringPlanViewList.Add(monitoringPlanView);
                }
                return SQLMonitoringPlanViewList;
            }
            catch (Exception ex)
            {
                throw new Exception("MonitoringPlanDAO Exception " + ex.Message);
            }
            finally
            {
                //if (conn != null) conn.Close();
                if (SQLconn != null)
                {
                    SQLconn.Close();
                }
            }
        }
    }
}